<template>
    <div>
        <div class="header">
            <el-card class="box-card pieOption">
            </el-card>
            <el-card class="box-card barOption">
            </el-card>
            
        </div>
        <div class="filter">
            <el-button>刷新</el-button>
            <el-button @click="reset">重置</el-button>
            <el-input placeholder="请输入设备名称" v-model="filterparams.pileName" style="width:300px"></el-input>
            <el-select v-model="filterparams.status" placeholder="请选择">
                <el-option v-for="item in chargingStatus" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
            </el-select>
            <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        </div>
        <el-card>
            <h3>充电桩一览表</h3>
            <div class="see">
                <div v-for="(item, index) in pileData.data" :key="index" class="charging">
                    <div class="left">
                        <img :src="item.status === 0 ? imgSrc.healthy : item.status === 1 ? imgSrc.zhanyong : imgSrc.danger" alt="">
                        <p style="text-align:center" :class="item.status === 0 ? 'green' : item.status === 1 ? 'blue' : 'red'">
                            {{ item.status === 0 ? '空闲' : item.status === 1 ? '占用' : '告警' }}</p>
                    </div>
                    <div class="right">
                        <p>设备名称：{{ item.pileName }}</p>
                        <p>充电电压：{{ item.voltage }}V</p>
                        <p>充电功率：{{ item.power }}W</p>
                        <p>运行时长：{{ item.duration }}</p>
                    </div>
                </div>
            </div>
        </el-card>
        <el-pagination background layout="prev, pager, next" :page-size='filterparams.size' :total="pileData.total" :current-page="filterparams.page" @current-change="pageChange"
            style="float:right">
        </el-pagination>
    </div>
</template>
<script>
import * as echarts from 'echarts';
import { pieOption, barOption } from './charging.config';
import API from '../../api'
export default {
    created() {
        this.getPileData(this.filterparams)
    },
    async mounted() {
        const data = await this.getPileData({})
        this.getPieEacharts(pieOption, data)
        this.getBarEacharts(barOption, data)
    },
    data() {
        return {
            infoPage: {
                page: 1,
                size: 6,
            },
            chargingStatus: [
                { label: '告警', value: 2 },
                { label: '占用', value: 1 },
                { label: '空闲', value: 0 },
            ],
            filterparams: {
                page: 1,
                size: 6,
            },
            pileData: {

            },
            imgSrc: {
                healthy: 'https://cimg.axureshop.com/62/54/8e/62548e81ca154ee48b9ac305d825d092/images/健康监控/u2365.png',
                danger: 'https://cimg.axureshop.com/62/54/8e/62548e81ca154ee48b9ac305d825d092/images/健康监控/u2384.png',
                zhanyong: 'https://cimg.axureshop.com/62/54/8e/62548e81ca154ee48b9ac305d825d092/images/健康监控/u2377.png'
            },
        }
    },
    methods: {
        async getPieEacharts(option, data) {
            option.series[0].data=[]
            var myChart = echarts.init(
                document.querySelector('.pieOption')
            );
            //统计data
            option.series[0].data.push(
                {
                    name: '空闲充电桩',
                    value: this.getStatusNum(data.data, 0)
                },
                {
                    name: '占用充电桩',
                    value: this.getStatusNum(data.data, 1)
                },
                {
                    name: '告警充电桩',
                    value: this.getStatusNum(data.data, 2)
                },
            )
            //中间总数
            option.graphic[0].style.text = `充电桩总数${data.total}`
            myChart.setOption(option);
        },
        async getBarEacharts(option, data) {
            option.yAxis.data=[]
            option.series[0].data=[]
            var myChart = echarts.init(
                document.querySelector('.barOption')
            );
            const failData = data.data
            failData.sort((a, b) => {
                return b.failNum - a.failNum
            })
            failData.slice(0, 10).forEach(val => {
                option.yAxis.data.unshift(`${val.pileName}`)
                option.series[0].data.unshift(`${val.failNum}`)
            })
            myChart.setOption(option);
        },

        async getPileData(page) {
            if(!(JSON.stringify(page)==='{}')){
                const data = await API.getPileData({ ...page })
                this.pileData = data
            }else {
                const data = await API.getPileData({ ...page })
                return data
            }
        },
        getStatusNum(data, num) {
            return data.filter(val => val.status === num).reduce((total, item) => total + 1, 0)
        },
        pageChange(page) {
            this.getPileData({ ...this.filterparams, page })
        },
        search(){
            if(this.filterparams.pileName||this.filterparams.status>=0){
                this.getPileData({...this.filterparams,page:1})
            }
            else{
                this.reset()
            }
        },
        reset(){
            this.filterparams={page:1,size:6}
            this.getPileData(this.filterparams)
        }
    }
}
</script>
<style scoped>
.header {
    display: flex;
    justify-content: space-around;
    margin-top: 20px;
}
.el-button {
    margin: 0 30px;
}
.el-input {
    margin: 0 30px;
}
.box-card {
    padding: 10px;
    box-sizing: border-box;
    width: 35%;
    height: 300px
}

.filter {
    display: flex;
    margin: 20px 0;
}

.showCharging {
    width: 100%;
    display: flex;
    justify-content: center;
}

.see {
    width: 80%;
    height: 400px;
    display: flex;
    justify-content: space-around;
    flex-wrap: wrap;
    margin: 20px auto;
}
.left {
    width: 144px;
}

.right {
    flex: 1;
    text-align: center;
}

.red {
    font-size: 18px;
    color: #DA2626;
}

.blue {
    font-size: 18px;
    color: #425AB3;
}

.green {
    font-size: 18px;
    color: green;
}

.charging {
    width: 30%;
    display: flex;
}
</style>